package model.events

import model.postgresql.{PgBaseEvent, PgWallRepost}
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder}
import zio.json.*

case class WallRepost(
    `object`: WallRepost.`object`,
    event_id: Option[String],
    timestamp: Option[Long]
) extends BaseEvent(event_id, timestamp):
  override def convertToJson: String              = this.toJson
  override def getUserId: Some[Long]              = Some(this.`object`.from_id)
  override def setTimestamp(ts: Long): WallRepost = copy(timestamp = Some(ts))

  override def toPg: Option[PgBaseEvent] =
    for {
      eventId   <- event_id
      timestamp <- timestamp
    } yield PgWallRepost(
      eventId,
      timestamp,
      `object`.text,
      `object`.id,
      `object`.post_type,
      `object`.from_id,
      `object`.`type`
    )

object WallRepost:

  case class `object`(
      `type`: String,
      date: Long,
      from_id: Long,
      id: Long,
      owner_id: Long,
      post_type: String,
      text: String
  )

  given JsonDecoder[`object`] = DeriveJsonDecoder.gen[`object`]
  given JsonEncoder[`object`] = DeriveJsonEncoder.gen[`object`]

  given JsonDecoder[WallRepost] = DeriveJsonDecoder.gen[WallRepost]
  given JsonEncoder[WallRepost] = DeriveJsonEncoder.gen[WallRepost]
